/*
 * Copyright (c) 2011, Andrew Cole
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.andycole.xmiparser.parser;

import net.andycole.xmiparser.transfer.ASTypes;

import java.util.Hashtable;
import java.util.HashSet;
import net.andycole.xmiparser.transfer.TransferClass;

/**
 * Base class for all Actionscript parsers.
 * @author Andrew Cole
 */
public class AbstractASParser
{
   protected Hashtable _classTable;
   protected HashSet<String> _asTypeTable;
   protected Hashtable<String, String> _importTypeTable;
   protected String _packageName;
   protected TransferClass _classInfo;
   
   public AbstractASParser()
   {
      _classTable = new Hashtable();
      _asTypeTable = new HashSet();
      for (ASTypes p : ASTypes.values()) {
         _asTypeTable.add(p.toString());
      }

      _importTypeTable = new Hashtable();
      _packageName = "";
      _classInfo = new TransferClass();
   }

   protected boolean isCustomType(String type)
   {
      if (_asTypeTable.contains(type))
         return false;
      return true;
   }

   protected String getQualifiedType(String type)
   {
      if (type == null)
         return "void";
      if (!isCustomType(type))
         return type;
      if (type.indexOf(".") > -1)
         return type;
      if (_importTypeTable.get(type) != null)
         return _importTypeTable.get(type);
      if (_packageName.length() == 0)
         return type;
      return _packageName + "." + type;
   }
}
